#define _CRT_SECURE_NO_WARNINGS 1
//*
// * Definition for a binary tree node.
struct TreeNode {
     int val;
     struct TreeNode *left;
     struct TreeNode *right;
};
 
int BinarySize(struct TreeNode* root)
{
    return root == NULL ? 0 : 1 + BinarySize(root->left)
        + BinarySize(root->right);
}

void preorder(struct TreeNode* root, int* a, int* pi)
{
    if (root == NULL)
        return;

    a[*pi] = root->val;
    (*pi)++;
    preorder(root->left, a, pi);
    preorder(root->right, a, pi);

}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    int i = 0;
    int* pi = &i;
    *returnSize = BinarySize(root);
    int* a = (int*)malloc(sizeof(int) * (*returnSize));

    preorder(root, a, pi);

    return a;
}